import http from './http.js'

const install = (Vue, vm) => {

	if (getApp().globalData.dev) {
		var baseUrl = getApp().globalData.api_url_dev
	} else {
		var baseUrl = getApp().globalData.api_url_prod
	}

	http.setConfig({
		baseUrl: baseUrl,
	});
	//请求拦截
	http.interceptor.request(function(type, config) {
		config.header.token = uni.getStorageSync('token')
		return config
	})
	//响应拦截
	http.interceptor.response(function(type, res) {
		if (type == 'request') {
			if (res.code == 1) {
				return res;
			} else if (res.code == 4) {
				return res;
			} else if (res.code == 9) {
				return res;
			} else if (res.code == 0) {
				uni.showToast({
					title: res.msg,
					icon: "none"
				});
				return res;
			} else if (res.code == -1) {

				// 获取当前打开过的页面路由数组
				let routes = getCurrentPages();
				let curRoute = routes[routes.length - 1].route;
				let curParam = routes[routes.length - 1].options;
				let param = ''
				for (let key in curParam) {
					param += '&' + key + '=' + curParam[key]
				}
				let retcurl = curRoute + '?' + param.replace(/^&*/, '')
				uni.setStorageSync('retcurl', retcurl);
				uni.navigateTo({
					url: '/pages/login/login'
				});
			}
		} else {
			let data = JSON.parse(res);
			return data;
		}

	}, function(type, err) {
		if (type == 'request') {
			uni.showToast({
				title: '系统错误',
				icon: "none"
			});

		}
		return err;
	})
	vm.$api.wechatLogin = (data = {}) => http.get('Third/wechatLogin', data);
	vm.$api.testLogin = (data = {}) => http.get('Third/testLogin', data); //调试登录
	vm.$api.getSite = (data = {}) => http.get('common/getSite', data);
	vm.$api.common_getAdv = (data = {}) => http.get('common/getAdv', data);
	vm.$api.user_aplogin = (data = {}) => http.post('user/aplogin', data, true, '登录中')
	vm.$api.user_logout = (data = {}) => http.post('user/logout', data, true, '请求中')


	//首页
	vm.$api.index = (data = {}) => http.get('index/index', data);
	// 发布
	vm.$api.goodsInit = (data = {}) => http.get('Goods/goodsinit', data); // 初始化
	vm.$api.goodsBrand = (data = {}) => http.get('Goods/get_brand_by_class', data); // 获取品牌
	vm.$api.goodsModel = (data = {}) => http.get('Goods/get_model_by_brand', data);
	// vm.$api.index = (data = {}) => http.get('index/index', data);
	// vm.$api.detail = (data = {}) => http.get('index/detail', data);
	// vm.$api.index_clue = (data = {}) => http.post('index/clue', data, true, '提交中');
	// vm.$api.index_jx = (data = {}) => http.get('index/jx', data);
	// vm.$api.index_artDetail = (data = {}) => http.get('index/artDetail', data);

	// //工作
	// vm.$api.job_index = (data = {}) => http.get('job/index', data);
	// vm.$api.job_detail = (data = {}) => http.get('job/detail', data);
	// //商家
	// vm.$api.shop_index = (data = {}) => http.get('shop/index', data);
	// vm.$api.shop_detail = (data = {}) => http.get('shop/detail', data);
	// vm.$api.shop_pro = (data = {}) => http.get('shop/pro', data);
	// //团队
	// vm.$api.team_pro = (data = {}) => http.get('team/pro', data);
	// vm.$api.team_join = (data = {}) => http.post('team/join', data, true, '提交中')

	// vm.$api.sms_send = (data = {}) => http.post('common/smsSend', data, true, '请求中')
	// vm.$api.user_uinfo = (data = {}) => http.get('user/uinfo', data, false)
	// vm.$api.change_mobile = (data = {}) => http.post('user/changeMobile', data, true, '提交中')
	// vm.$api.user_uinfo_post = (data = {}) => http.post('user/uinfo', data, true, '提交中')

	// //发布
	// vm.$api.info_fabu = (data = {}) => http.get('info/fabu', data, false)
	// vm.$api.info_fabu_sub = (data = {}) => http.get('info/fabuSub', data)
	// vm.$api.info_fabu_subpost = (data = {}) => http.post('info/fabuSub', data, true, '发布中')
	// vm.$api.info_fabu_list = (data = {}) => http.post('info/fabuList', data, )
	// vm.$api.info_fabu_act = (data = {}) => http.post('info/fabuAct', data, true, '请求中')
	// vm.$api.info_list = (data = {}) => http.get('info/list', data)
	// vm.$api.info_listIndex = (data = {}) => http.get('info/listIndex', data, false)
	// vm.$api.info_detail = (data = {}) => http.get('info/detail', data)
	// vm.$api.info_like = (data = {}) => http.post('info/infoLike', data, false)
	// vm.$api.index_artDetail = (data = {}) => http.get('index/artDetail', data)
	// vm.$api.info_fabu_listadmin = (data = {}) => http.post('info/fabuListAdmin', data, )
	// vm.$api.info_fabu_actadmin = (data = {}) => http.post('info/fabuActAdmin', data, true, '请求中')
	// vm.$api.info_fabu_subadmin = (data = {}) => http.get('info/fabuSubAdmin', data)
	// vm.$api.info_fabu_subadminpost = (data = {}) => http.post('info/fabuSubAdmin', data)

	// //线下收款
	// vm.$api.pay_index = (data = {}) => http.get('pay/index', data);
	// vm.$api.pay_create = (data = {}) => http.post('pay/create', data);
	// vm.$api.pay_qr = (data = {}) => http.get('pay/getQr', data);
}
export default {
	install
}
